package me.xplabs.model 
{
	import flash.geom.Point;
	import flash.utils.ByteArray;
	import me.xplabs.Globals;
	import me.xplabs.model.combatunit.BaseBuff;
	import me.xplabs.model.combatunit.BaseBuild;
	import me.xplabs.model.combatunit.BaseFlag;
	import me.xplabs.model.combatunit.BaseRole;
	import me.xplabs.model.combatunit.MBuildInfo;
	import me.xplabs.model.combatunit.MRoleInfo;
	import me.xplabs.model.energy.EnergyData;
	import me.xplabs.model.msg.MsgAddBuffActor;
	import me.xplabs.model.msg.MsgDelBuffActor;
	import me.xplabs.model.msg.MsgHitActor;
	import me.xplabs.model.proxy.EnergyWaveProxy;
	import me.xplabs.model.proxy.MMapManagerProxy;
	import me.xplabs.model.skill.BaseSkill;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	/**
	 * ...
	 * @author xiaohan
	 */
	public class MMapManagerCommand extends SimpleCommand 
	{
		public function MMapManagerCommand() 
		{
		}
		override public function execute(notification:INotification):void 
		{
			switch(notification.getType())
			{
				case NotificationFight.APPEAR_ENERGY:
					var tempEnergy:EnergyData = EnergyData(notification.getBody());
					var tempRole:BaseRole = GameDataSingleton.i.getBaseRoleById(tempEnergy.energyId);
					MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).addActor(tempEnergy, tempRole);
					break;
				case NotificationFight.DISAPPEAR_ENERGY:
					MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).delActor(int(notification.getBody()));
					break;
				case NotificationFight.UPGRADE_BUILD_COMPLETE:
					var baseBuild:BaseBuild = GameDataSingleton.i.getBaseBuildById(notification.getBody().upgradeId);
					//var baseFlag:BaseFlag = GameDataSingleton.i.getBaseFlagById(baseBuild.flag);
					var skills:Vector.<BaseSkill> = new Vector.<BaseSkill>();
					for (var j:int = 0; j < baseBuild.skillIds.length; j++) 
					{
						skills.push(GameDataSingleton.i.getBaseSkillById((baseBuild.skillIds[j])));
					}
					MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).upgradeBuilding(notification.getBody().soleId, baseBuild, null, skills);
					break;
				case NotificationFight.INIT_BUILD_LIST:
					/*var builds:Vector.<MBuildInfo> = Vector.<MBuildInfo>(notification.getBody());
					var len:int = builds.length;
					for (var i:int = 0; i < len; i++) 
					{
						baseBuild =GameDataSingleton.i.getBaseBuildById(int(builds[i].resId));
						baseFlag = GameDataSingleton.i.getBaseFlagById(baseBuild.flag);
						skills = new Vector.<BaseSkill>();
						for (j = 0; j < baseBuild.skillIds.length; j++) 
						{
							skills.push(GameDataSingleton.i.getBaseSkillById(baseBuild.skillIds[j]));
						}
						MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).addBuilding(baseBuild, baseFlag, builds[i].cellX, builds[i].cellY, skills);
					}*/
					break;
				case NotificationFight.HIT_ACTOR:
					MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).hitActor(MsgHitActor(notification.getBody()));
					break;
				case NotificationFight.DESTORY_BUILD_COMPLETE:
					MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).destoryBuilding(int(notification.getBody()));
					break;
				case NotificationFight.LAST_ENERGY_WAVE:
					MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).isLastWave = true;
					break;
				case NotificationFight.SUSPEND_GAME:
					//MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).stopUpdate();
					Globals.gameManager.stopDataUpdate();
					break;
				case NotificationFight.REGAIN_GAME:
					Globals.gameManager.startDataUpdate();
					//MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).startUpdate();
					break;
				case NotificationFight.QUIT_GAME:
					if (facade.retrieveProxy(EnergyWaveProxy.NAME)) EnergyWaveProxy(facade.retrieveProxy(EnergyWaveProxy.NAME)).disposeEnergyData();
					if (facade.retrieveProxy(MMapManagerProxy.NAME)) MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).dispose();
					break;
				case NotificationFight.ADD_FLAG_COMPLETE:
					
					var baseFlag:BaseFlag = GameDataSingleton.i.getBaseFlagByTypeAndLevel(notification.getBody().flagType, notification.getBody().flagLevel);
					var tempPt:Point = notification.getBody().point;
					if (facade.retrieveProxy(MMapManagerProxy.NAME)) MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).addFlag(baseFlag, tempPt.x, tempPt.y);
					break;
				case NotificationFight.REMOVE_FLAG_COMPLETE:
					break;
				case NotificationFight.ADD_BUFF:
					var baseBuff:BaseBuff = GameDataSingleton.i.getBaseBuffById(MsgAddBuffActor(notification.getBody()).buffId);
					if (facade.retrieveProxy(MMapManagerProxy.NAME)) MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).addBuff(MsgAddBuffActor(notification.getBody()).rolesId, baseBuff);
					break;
				case NotificationFight.DEL_BUFF:
					if (facade.retrieveProxy(MMapManagerProxy.NAME)) MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).delBuff(MsgDelBuffActor(notification.getBody()).roleId, MsgDelBuffActor(notification.getBody()).baseId);
					break;
			}
		}
	}

}